<!DOCTYPE html>
<html>
<head>
<title>Op-Amp Circuit Diagram</title>
</head>
<body>
<canvas id="circuitCanvas" width="600" height="450" style="border:1px solid #d3d3d3;"></canvas>
<script>
    const canvas = document.getElementById('circuitCanvas');
    const ctx = canvas.getContext('2d');

    // Style settings
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.font = '16px Arial';
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    // Helper function to draw a resistor (rectangle)
    function drawResistor(x, y, width, height) {
        ctx.strokeRect(x - width / 2, y - height / 2, width, height);
    }

    // Helper function to draw a terminal (circle)
    function drawTerminal(x, y) {
        ctx.beginPath();
        ctx.arc(x, y, 4, 0, 2 * Math.PI);
        ctx.fillStyle = 'white';
        ctx.fill();
        ctx.stroke();
        ctx.fillStyle = 'black';
    }
    
    // Helper function to draw a junction (dot)
    function drawJunction(x, y) {
        ctx.beginPath();
        ctx.arc(x, y, 3, 0, 2 * Math.PI);
        ctx.fill();
    }

    // Helper function to draw an arrow head
    function drawArrowHead(x, y, angle) {
        const headLength = 8;
        ctx.beginPath();
        ctx.moveTo(x, y);
        ctx.lineTo(x - headLength * Math.cos(angle - Math.PI / 6), y - headLength * Math.sin(angle - Math.PI / 6));
        ctx.moveTo(x, y);
        ctx.lineTo(x - headLength * Math.cos(angle + Math.PI / 6), y - headLength * Math.sin(angle + Math.PI / 6));
        ctx.stroke();
    }


    // --- Component Coordinates ---
    const opamp_in_x = 220;
    const opamp_out_x = 320;
    const opamp_mid_y = 190;
    const opamp_h = 120;
    const opamp_in_plus_y = opamp_mid_y + 30;
    const opamp_in_minus_y = opamp_mid_y - 30;

    const ground_y = 350;
    const vin_x = 80;
    const vout_x = 550;
    
    const r_800_x = 160;
    const feedback_y = 90;
    const led_x = 450;
    
    // --- Draw Wires ---
    ctx.beginPath();
    // Ground line
    ctx.moveTo(vin_x, ground_y);
    ctx.lineTo(vout_x, ground_y);

    // Input V_IN to non-inverting input
    ctx.moveTo(vin_x, opamp_in_plus_y);
    ctx.lineTo(opamp_in_x, opamp_in_plus_y);

    // Feedback path
    ctx.moveTo(opamp_out_x, opamp_mid_y);
    ctx.lineTo(380, opamp_mid_y);
    ctx.lineTo(380, feedback_y);
    ctx.lineTo(r_800_x, feedback_y);
    ctx.lineTo(r_800_x, opamp_in_minus_y);
    ctx.lineTo(opamp_in_x, opamp_in_minus_y);
    
    // 800 Ohm resistor path
    ctx.moveTo(r_800_x, opamp_in_minus_y);
    ctx.lineTo(r_800_x, ground_y);
    
    // Output path to V_OUT and LED
    ctx.moveTo(opamp_out_x, opamp_mid_y);
    ctx.lineTo(vout_x, opamp_mid_y);
    ctx.moveTo(led_x, opamp_mid_y);
    ctx.lineTo(led_x, ground_y);
    
    // Power supply lines
    ctx.moveTo(270, opamp_mid_y - opamp_h/2);
    ctx.lineTo(270, opamp_mid_y - opamp_h/2 - 20);
    ctx.moveTo(270, opamp_mid_y + opamp_h/2);
    ctx.lineTo(270, opamp_mid_y + opamp_h/2 + 20);

    ctx.stroke();

    // --- Draw Components ---
    
    // Op-Amp
    ctx.beginPath();
    ctx.moveTo(opamp_in_x, opamp_mid_y - opamp_h / 2); // top corner
    ctx.lineTo(opamp_in_x, opamp_mid_y + opamp_h / 2); // bottom corner
    ctx.lineTo(opamp_out_x, opamp_mid_y); // output corner
    ctx.closePath();
    ctx.stroke();

    // Resistors
    drawResistor(300, feedback_y, 80, 20); // 4.0 kOhm
    drawResistor(r_800_x, 260, 20, 50); // 800 Ohm
    drawResistor(led_x, 300, 20, 50); // Resistor R

    // LED
    // Diode triangle
    ctx.beginPath();
    ctx.moveTo(led_x, 230); // Cathode line
    ctx.lineTo(led_x, 210);
    ctx.moveTo(led_x - 10, 210);
    ctx.lineTo(led_x + 10, 210);
    ctx.lineTo(led_x, 230);
    ctx.closePath();
    ctx.stroke();
    // Light arrows
    ctx.beginPath();
    ctx.moveTo(led_x + 8, 212);
    ctx.lineTo(led_x + 18, 202);
    drawArrowHead(led_x + 18, 202, Math.PI / 4);
    ctx.moveTo(led_x + 12, 222);
    ctx.lineTo(led_x + 22, 212);
    drawArrowHead(led_x + 22, 212, Math.PI / 4);
    ctx.stroke();

    // Ground Symbol
    const ground_x = led_x-10;
    ctx.beginPath();
    ctx.moveTo(ground_x, ground_y);
    ctx.lineTo(ground_x, ground_y + 20);
    ctx.moveTo(ground_x - 15, ground_y + 20);
    ctx.lineTo(ground_x + 15, ground_y + 20);
    ctx.moveTo(ground_x - 10, ground_y + 25);
    ctx.lineTo(ground_x + 10, ground_y + 25);
    ctx.moveTo(ground_x - 5, ground_y + 30);
    ctx.lineTo(ground_x + 5, ground_y + 30);
    ctx.stroke();
    
    // --- Draw Terminals and Junctions ---
    drawTerminal(vin_x, opamp_in_plus_y);
    drawTerminal(vout_x, opamp_mid_y);
    drawJunction(opamp_out_x, opamp_mid_y);
    drawJunction(r_800_x, opamp_in_minus_y);
    drawJunction(led_x, opamp_mid_y);
    drawJunction(r_800_x, ground_y);
    drawJunction(led_x, ground_y);

    // --- Draw V_IN and V_OUT arrows ---
    // V_IN
    ctx.beginPath();
    ctx.moveTo(vin_x, ground_y);
    ctx.lineTo(vin_x, opamp_in_plus_y + 5);
    drawArrowHead(vin_x, opamp_in_plus_y + 5, -Math.PI/2);
    ctx.stroke();
    
    // V_OUT
    ctx.beginPath();
    ctx.moveTo(vout_x, ground_y);
    ctx.lineTo(vout_x, opamp_mid_y + 5);
    drawArrowHead(vout_x, opamp_mid_y + 5, -Math.PI/2);
    ctx.stroke();

    // --- Add Labels ---
    ctx.textAlign = 'center';
    ctx.fillText('+', opamp_in_x + 10, opamp_in_plus_y);
    ctx.fillText('−', opamp_in_x + 10, opamp_in_minus_y - 2);
    
    ctx.textAlign = 'left';
    ctx.fillText('+5.0 V', 275, opamp_mid_y - opamp_h/2 - 10);
    ctx.fillText('-5.0 V', 275, opamp_mid_y + opamp_h/2 + 10);
    
    ctx.textAlign = 'center';
    ctx.fillText('4.0 k\u03A9', 300, feedback_y - 20);
    
    ctx.textAlign = 'left';
    ctx.fillText('800 \u03A9', r_800_x + 15, 260);
    ctx.fillText('R', led_x + 15, 300);

    ctx.textAlign = 'right';
    ctx.fillText('V', vin_x - 10, (opamp_in_plus_y + ground_y) / 2 - 5);
    ctx.fillText('IN', vin_x - 10, (opamp_in_plus_y + ground_y) / 2 + 10);

    ctx.textAlign = 'left';
    ctx.fillText('V', vout_x + 10, (opamp_mid_y + ground_y) / 2 - 5);
    ctx.fillText('OUT', vout_x + 10, (opamp_mid_y + ground_y) / 2 + 10);

    ctx.textAlign = 'center';
    ctx.fillText('Fig. 7.1', canvas.width / 2, 420);

</script>
</body>
</html>